Retrieval Augmented Generation

What is Retrieval-Augmented Generation (RAG), and why use it?


  • RAG retrieves relevant external information and includes it in an LLM prompt before generating an answer; the model does not have to rely solely on training knowledge.
  • Useful for private/domain-specific knowledge, frequently updated facts, and answers supported by references, without retraining a foundation model.
  • RAG can reduce hallucinations but does not guarantee correctness: irrelevant retrieval, outdated sources, or faulty reasoning can still produce wrong answers.
  • Unlike fine-tuning (changing model weights), RAG changes the context supplied at inference time. The approaches can complement each other.

How does a RAG pipeline work end to end?


  • Prepare/index: Load source documents; clean and split long documents into meaningful chunks; create numerical embeddings for chunks; store vectors, text, IDs, and useful metadata in a vector index.
  • Retrieve: Embed the user’s question with a compatible embedding model; search for semantically similar chunks; optionally apply metadata filters, hybrid keyword/vector search, and reranking.
  • Augment: Put the most relevant retrieved passages into a prompt with the question and instructions to answer from the evidence and cite sources.
  • Generate: An LLM produces an answer. Include source identifiers or links where available and instruct it to say when evidence is insufficient.
  • Evaluate/improve: Measure retrieval relevance, answer correctness, groundedness, latency, and cost; use feedback to refine chunking, filters, prompts, and indexing.
  • Flow: Documents → chunks → embeddings → vector index; Question → retrieval → context + question → LLM → answer.

How does Mosaic AI Vector Search work in Databricks?


  • Mosaic AI Vector Search provides managed vector search integrated with Delta tables and Unity Catalog governance.
  • Main resources: a Vector Search endpoint (serves queries) and a Vector Search index (stores searchable vectors and associated data). Managed-embedding configurations also use an embedding model endpoint; this may be Databricks-provided or separately configured depending on the setup.
  • Delta Sync index: Syncs data from a Delta source table. With Databricks-managed embeddings, specify the source text column and embedding model; Databricks computes embeddings. With self-managed embeddings, supply your own embedding vector column and its dimension.
  • Direct Access index: Your application explicitly inserts, updates, and deletes vectors rather than syncing from a Delta source.
  • For Delta Sync, Triggered sync requires manually or programmatically initiating a sync; Continuous sync maintains updates more frequently where supported. A triggered index is not automatically current after source changes.
  • Source tables may need Delta Change Data Feed enabled for the selected index configuration. Check endpoint/index readiness and permissions before querying.

How do you inspect and test a Vector Search index?


  • In Catalog Explorer, locate the index created by course setup (under dbacademy.get_started_genai), verify its state is ready, and inspect its source, primary key, embedding settings, and sync mode.
  • The setup course uses a product index such as product_embeddings, based on product text and a Databricks embedding model. Confirm the actual full index name and endpoint in your workspace rather than assuming them.
  • From Python, use the Vector Search SDK to open the existing index and test retrieval (replace placeholders with your actual resource names):
from databricks.vector_search.client import VectorSearchClient

client = VectorSearchClient()
index = client.get_index(
    endpoint_name="<vector-search-endpoint>",
    index_name="dbacademy.get_started_genai.product_embeddings"
)
results = index.similarity_search(
    query_text="wireless headphones",
    columns=["id", "text"],  # replace with actual indexed column names
    num_results=5
)
print(results)
  • query_text is suitable when the index is configured to embed queries. For an index using precomputed vectors, use query_vector with an embedding from the compatible model instead.
  • Review returned text, IDs, scores and metadata; inspect whether results truly answer the question before adding them to an LLM prompt.

How do you build and test a RAG chain?


  • Retriever: Search the Vector Search index using the user’s question and return relevant document passages.
  • Prompt: Include clear instructions, the retrieved passages, and the user question. Example:
Answer the question using only the provided context.
If the context does not contain the answer, say you don't know.
Cite the source IDs when possible.

Context:
{retrieved_passages}

Question: {user_question}
  • Generator: Call a Databricks-hosted foundation model using the augmented prompt; a framework such as LangChain can connect retrieval, formatting, and generation.
  • Test with answerable, unanswerable, and ambiguous questions. Check retrieved passages separately from the final answer to distinguish retrieval errors from generation errors.
  • Never place untrusted retrieved text above higher-priority instructions; documents are data, not instructions to the agent. Respect token limits and access controls.

How do MLflow and Unity Catalog support RAG development and deployment?


  • MLflow Tracking records parameters, metrics, artifacts, and source/configuration information for reproducible experiments.
  • MLflow Tracing shows stages such as retrieval, prompt construction, and model calls, including intermediate inputs/outputs, latency, and available token/cost information.
  • MLflow model logging packages the RAG application (for example, a LangChain chain) with dependencies and configuration for reuse and deployment.
  • Register the logged model in the Unity Catalog Model Registry under a three-part name such as dbacademy.get_started_genai.<rag_model_name>; versions, permissions, lineage, and aliases (for example, @champion) support lifecycle management.
  • A registered model can be loaded for notebook inference or deployed to a Mosaic AI Model Serving endpoint. Registration alone does not deploy an endpoint.
  • Evaluate both retrieval quality and answer quality before deployment; continue monitoring after deployment.

What are the practical steps for the course demo?


  • Run the earlier Get Started with Generative AI setup notebook first; it creates the sample product data, Vector Search resources, and initial serving infrastructure. Wait until provisioning completes before testing retrieval.
  • Inspect the product source table and the existing Vector Search index. The demo may create a separate example source table to explain index creation, but you need not duplicate the setup index.
  • In the index creation UI, understand the required endpoint, source table, primary key, indexed columns, embedding model/vector column, and sync mode.
  • Run a similarity search for a sample product question and inspect the returned records.
  • Connect retrieval to a Databricks-hosted LLM in a LangChain RAG chain; test end to end.
  • Log the chain using MLflow and register it in Unity Catalog; confirm the registered version appears in Catalog Explorer.
  • In Free Edition, check limits on Vector Search and Model Serving endpoints before creating additional resources; provisioning can take significant time.

Where can you find official reference material?